/*
* Copyright(C) 2009, kesalin All rights reserved.
* 
* File name : UStdStream.h
* Brief     : StdStream.
* Version   : 1.0
* Author    : kesalin@gmail.com
* Date      : 2009-6-5
*/

#pragma once


#include <string>
#include <vector>
#include <deque>
#include <map>
#include <sstream>

#include "UCommon.h"

namespace LuoUtils {

// OutStringStream
class OutStringStream {

	std::wostringstream m_oss;

public:

	void Append(LPCWSTR p, int len)
	{
		m_oss.write(p, len);
	}

	void Append(WCHAR ch)
	{
		m_oss.put(ch);
	}

	CStringW ToString()
	{
		std::wstring str = m_oss.str();
		return CStringW(str.c_str());
	}

}; // OutStringStream

/* */

// UTF8
class UTF8 {

	UTF8()
	{
	}

public:

	static CStringW ToString(
		const char* p)
	{
		int alen = lstrlenA(p);
		int wlen = MultiByteToWideChar(
			CP_UTF8,
			0,
			p,
			alen,
			0,
			0);

		CStringW wstr;

		LPWSTR wtext = wstr.GetBuffer(wlen);

		wlen = MultiByteToWideChar(
			CP_UTF8,
			0,
			p,
			alen,
			wtext,
			wlen);

		wstr.ReleaseBuffer(wlen);

		return wstr;
	}

	static CStringA ToBytes(
		LPCWSTR p)
	{
		int wlen = lstrlenW(p);
		int alen = WideCharToMultiByte(
			CP_UTF8,
			0,
			p,
			wlen,
			0,
			0,
			0,
			0);

		CStringA astr;

		char* atext = astr.GetBuffer(alen);

		alen = WideCharToMultiByte(
			CP_UTF8,
			0,
			p,
			wlen,
			atext,
			alen,
			0,
			0);

		astr.ReleaseBuffer(alen);

		return astr;
	}

}; // UTF8

/* */

} // namespace LuoUtils


